package com.frank.subject.common.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/1/26 12:50
 * @description 分页返回实体
 */
@Data
public class PageResult<T> implements Serializable {

    /**
     * 当前页
     */
    private Integer current = 1;

    /**
     * 每页条数
     */
    private Integer pageSize = 20;

    /**
     * 总条数
     */
    private Integer total = 0;

    /**
     * 总页数
     */
    private Integer totalPage = 0;

    /**
     * 起始
     */
    private Integer start = 1;

    /**
     * 终止
     */
    private Integer end = 1;

    /**
     * 数据列表
     */
    private List<T> list = Collections.emptyList();

    public void setRecords(List<T> records) {
        this.list = records;
        if (records != null && !records.isEmpty()) {
            setTotal(records.size());
        }
    }

    public void setTotal(Integer total) {
        this.total = total;
        if (this.pageSize > 0) {
            this.totalPage = total % this.pageSize == 0 ? total / this.pageSize : total / this.pageSize + 1;
        } else {
            this.totalPage = 0;
        }
        this.start = (this.pageSize > 0 ? (this.current - 1) * this.pageSize : 0) + 1;
        this.end = (this.start - 1 + this.pageSize * (this.current > 0 ? 1 : 0));
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
}
